package com.enation.app.shop.front.policyInsurance.adminController;

import com.enation.app.base.core.service.IMemberManager;
import com.enation.app.shop.front.api.inpolicy.service.impl.WarrantyManager;
import com.enation.app.shop.front.policyInsurance.entity.InsuranceBankCard;
import com.enation.app.shop.front.policyInsurance.layuiGrid.LayuiResponse;
import com.enation.app.shop.front.policyInsurance.layuiGrid.LayuiUtil;
import com.enation.app.shop.front.policyInsurance.service.InsuranceAreaMapper;
import com.enation.app.shop.front.policyInsurance.service.InsuranceBankCardMapper;
import com.enation.framework.action.JsonResult;
import com.enation.framework.util.JsonResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.List;

/**
 * 关于银行卡
 */
@Scope("prototype")
@Controller
@RequestMapping("/app/BankCard")
public class InsuranceBankCradController {
    @Autowired
    private InsuranceBankCardMapper insuranceBankCardMapper;
    @Autowired
    private IMemberManager memberManager;
    @Autowired
    private InsuranceAreaMapper insuranceAreaMapper;
    @Autowired
    private WarrantyManager warrantyManager;

    /**
     * 通过银行卡id得到银行卡详细信息
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/detail")
    public JsonResult getById(Long id){
       InsuranceBankCard insuranceBankCard= insuranceBankCardMapper.selectByPrimaryKey(id);
       if (insuranceBankCard!=null){
           return JsonResultUtil.getObjectMessageJson(insuranceBankCard,"查询银行卡详细成功");
       }else {
           return JsonResultUtil.getErrorJson("查询银行卡详细失败");
       }

    }



    //添加银行卡接口
    @ResponseBody
    @RequestMapping("/addbancard")
    public JsonResult createBankCard(@RequestBody  InsuranceBankCard insuranceBankCard){
        InsuranceBankCard insuranceBankCard1= new InsuranceBankCard();
        insuranceBankCard1.setAccount_type(insuranceBankCard.getAccount_type());
        insuranceBankCard1.setBank_card(insuranceBankCard.getBank_card());
        insuranceBankCard1.setCreate_at(new Timestamp(System.currentTimeMillis()));
        Integer userid=insuranceBankCard.getUser_id();
        List<InsuranceBankCard> insuranceBankCards=  insuranceBankCardMapper.selectByUserId(insuranceBankCard.getUser_id());
        for (InsuranceBankCard insuranceBankCard2:insuranceBankCards){
            if (insuranceBankCard2.getBank_card().equals(insuranceBankCard.getBank_card())){
                return JsonResultUtil.getErrorJson("您已经添加过此银行卡了。");
            }
        }
        insuranceBankCard1.setBank_user_name(insuranceBankCard.getBank_user_name());
        insuranceBankCard1.setOpening_bank(insuranceBankCard.getOpening_bank());
        insuranceBankCard1.setUrl(insuranceBankCard.getUrl());
        insuranceBankCard1.setUser_id(insuranceBankCard.getUser_id());
        insuranceBankCard1.setIpc(insuranceBankCard.getIpc());
        insuranceBankCard1.setStatus(1);
        int i=insuranceBankCardMapper.insert(insuranceBankCard1);
        if (i>0){
            return JsonResultUtil.getSuccessJson("添加银行卡成功");
        }else {
            return JsonResultUtil.getErrorJson("添加银行卡失败");
        }
    }


    //银行卡列表
    @ResponseBody
    @RequestMapping("/getlist")
    public JsonResult getList(Integer userid){
      //  List<InPolicy> inPolicies=warrantyManager.getWarrantyByuserid(userid);
      List<InsuranceBankCard> insuranceBankCards= insuranceBankCardMapper.selectByUserId(userid);
      if (insuranceBankCards==null){
          return JsonResultUtil.getErrorJson("您还没有银行卡");
      }else {
          return JsonResultUtil.getObjectMessageJson(insuranceBankCards,"银行卡列表");
      }
    }


    //删除银行卡，更新状态
    @ResponseBody
    @RequestMapping("/deletecard")
    public JsonResult deletecard(Long id,Integer userid){
        List<InsuranceBankCard> insuranceBankCards=insuranceBankCardMapper.selectByUserId(userid);
        InsuranceBankCard insuranceBankCard1=insuranceBankCardMapper.selectByPrimaryKey(id);
        if (insuranceBankCard1.getUser_id()!=(long)userid){
            return JsonResultUtil.getErrorJson("您没有权限");
        }
        insuranceBankCard1.setStatus(0);//0不用  1在使用
        try {
            insuranceBankCardMapper.updateByPrimaryKey(insuranceBankCard1);
            return JsonResultUtil.getSuccessJson("删除成功");
        }catch (Exception e){
            return JsonResultUtil.getErrorJson("删除失败");
        }
    }



    /**
     * web端银行卡列表显示
     * @return
     */
    @ResponseBody
    @RequestMapping("/getbankcardlist")
    public LayuiUtil getAllTemplatetest(Integer page, Integer limit, String seacher){
        if (seacher==null){
            List<InsuranceBankCard> insuranceBankCards=insuranceBankCardMapper.getAll();
            List<InsuranceBankCard> insuranceBankCardList= insuranceBankCardMapper.getByPage((page-1)*limit,limit,seacher);
            return LayuiResponse.getObjectMessageJson(insuranceBankCardList,"ss",insuranceBankCards.size());
        }else {
            List<InsuranceBankCard> insuranceBankCards=insuranceBankCardMapper.getBySercher(seacher);
            List<InsuranceBankCard> insuranceBankCardList= insuranceBankCardMapper.getByPage((page-1)*limit,limit,seacher);
            return LayuiResponse.getObjectMessageJson(insuranceBankCardList,"ss",insuranceBankCards.size());
        }

    }





}
